#!/usr/bin/env python
# -*- coding:utf-8 -*-
import configparser
import json

from conn.MyRedisFactory import RedisConfig
from excel.EGC.EgcBaseTabTop import EgcBaseTabTop
from excel.EGC.EgcDictUtil import EgcDictUtil
from excel.PruTuutDb import PruTuutDb

f = r'setting.ini'
config = configparser.RawConfigParser()
config.read(f, encoding='UTF8')
rds_cfg = RedisConfig(host=config['redis']['host'],
                      port=config['redis']['port'],
                      passwd=config['redis']['password'])
rds_conn = RedisConfig.get_pool(rds_cfg)

base_table_top = EgcBaseTabTop()
pru_tuut_db_conn = PruTuutDb.get_pool()
dict_db = EgcDictUtil(pru_tuut_db_conn)
# **
# 参考计算EXCEL原文件中列表右侧的数据列
# ----------------------------------------------------------------------------------------------------------
TEST_YEAR = range(100)  # 测试年限为100的数据


# ----------------------------------------------------------------------------------------------------------
class ###AclassName###BaseTabCalStd:
    res = []

    def __init__(self):
        return

    # 粘贴生成的方法内容 -------------------------------------------------------------------------------------------- START
    ## METHOD ##
    # 粘贴生成的方法内容 --------------------------------------------------------------------------------------------   END

    # **
    # 说明:
    @staticmethod
    def ###AclassName###_master(str_json_key, args: list[str], years):
        from datetime import datetime
        TEST_SELECT_ANB = float(args[0])  # 翌年歲
        TEST_ANNUAL_SAVING = float(args[1])  # 每年儲蓄金額
        TEST_SAVING_PAYMENT_TERM = float(args[2])  # 儲蓄供款期
        TEST_REPEAT_SAVING_PLAN = float(args[3])  # 重複儲蓄計劃
        TEST_REPEAT_SAVING_GROWTH_RATE = float(args[4])  # 重複儲蓄增長率
        TEST_SAVING_SUM_ASSURED = float(args[5])  # 儲蓄名義金額

        TEST_CURRENCY = float(args[6])  # 貨幣
        TEST_SURRENDER_ANB = float(args[7])  # 退保翌年歲
        TEST_WITHDRAWAL_STARTING_ANB = float(args[8])  # 提款開始翌年歲
        TEST_FST_YEAR_WITHDRAWAL = float(args[9])  # 首年提款
        TEST_INFLATION = float(args[10])  # 通脹率
        TEST_WITHDRAWAL_YEARS = float(args[11])  # 提取年期
        TEST_PROMOTION = float(args[12])  # 推廣優惠
        TEST_LUMP_SUM_PREPAID = float(args[13])  # 一筆過預繳
        calStd = ###AclassName###BaseTabCalStd()
        try:
            # 获取外部参数
            cal_display_rate = base_table_top.get_Display_rate(currency=TEST_CURRENCY)
            cal_sa_minimum = base_table_top.get_SA_minimum(currency=TEST_CURRENCY)
            cal_lumsum = base_table_top.get_lumsum()
            cal_lumpsum_discount = base_table_top.get_lumpsum_discount()
            # ----------------------------------------------------------------------------------------------------------
            rows = []
            last_Remain = 1  # 这里是循环数据处理的前置数据
            now = datetime.now()
            current_time = now.strftime("%H:%M:%S")
            print("当前时间 =", current_time)
            save_db_flg = True
            # ----------------------------------------------------------------------------------------------------------
            for test_year in years:
                single_row_data = {}  # 页面数据行
                anb_year = (TEST_SELECT_ANB) + test_year  # 计算投资起始年龄
                if test_year == 0:
                    continue
                # 获取复制的业务代码内容------------------------------------------------------------------------------ START
                ## CALLING ##
                # 获取复制的业务代码内容------------------------------------------------------------------------------   END
                rows.append(single_row_data)
            # ----------------------------------------------------------------------------------------------------------
            now_2 = datetime.now()
            current_time_2 = now_2.strftime("%H:%M:%S")
            print("当前时间 =", current_time_2)
            str_json_data = json.dumps(rows, ensure_ascii=False)
            if save_db_flg:
                dict_db.insert_cal_json_data([str_json_key, str_json_data, str(datetime.now().strftime("%f"))])
            # 保持一致同步，写入到RDS中
            RedisConfig.push_val_intimes(rds_cfg, key=f'exceL_hash_key:{str_json_key}', val=str_json_data,
                                         json_flg=True, inTimes=60 * 60)
            return {"code": 200, "data": str_json_data}
        except Exception as e:
            print(e)
